package com.guoli.sms.service.impl;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.guoli.sms.dao.IDeptDao;
import com.guoli.sms.dao.IUserPhoneDao;
import com.guoli.sms.dao.impl.DeptDaoImpl;
import com.guoli.sms.dao.impl.UserPhoneDaoImpl;
import com.guoli.sms.service.IUserPhoneService;
import com.guoli.sms.util.DBTool;
import com.guoli.sms.vo.UserPhone;

public class UserPhoneServiceImpl implements IUserPhoneService {
	
	private IUserPhoneDao userPhoneDao;
	private IDeptDao deptDao;
	private DBTool dbTool;
	
	public UserPhoneServiceImpl(){
		userPhoneDao = new UserPhoneDaoImpl();
		deptDao = new DeptDaoImpl();
		dbTool = DBTool.newInstance();
	}
	
	@Override
	public List<UserPhone> getUserPhones() {
		Connection conn = dbTool.getConnection();
		List<UserPhone> userPhones = null;
		try {
			conn.setAutoCommit(false);
			userPhones = userPhoneDao.getUserPhones(conn);
			for(UserPhone up : userPhones){
				up.setDept(deptDao.getDeptById(up.getDept().getId(), conn));
			}
			conn.commit();
			conn.setAutoCommit(true);
		} catch (Exception e) {
			try {
				conn.rollback();
				conn.setAutoCommit(true);
			} catch (SQLException e1) {
				System.out.println("回滚失败...");
				e1.printStackTrace();
			}
			System.out.println("查询所有用户信息失败...");
			e.printStackTrace();
		} finally{
			dbTool.closeConn(conn);
		}
		return userPhones;
	}

	@Override
	public List<UserPhone> getUserPhonesByDept(int dept_id) {
		Connection conn = dbTool.getConnection();
		List<UserPhone> userPhones = null;
		try {
			conn.setAutoCommit(false);
			userPhones = userPhoneDao.getUserPhonesByDept(dept_id, conn);
			for(UserPhone up : userPhones){
				up.setDept(deptDao.getDeptById(up.getDept().getId(), conn));
			}
			conn.commit();
			conn.setAutoCommit(true);
		} catch (Exception e) {
			try {
				conn.rollback();
				conn.setAutoCommit(true);
			} catch (SQLException e1) {
				System.out.println("回滚失败...");
				e1.printStackTrace();
			}
			System.out.println("查询某部门的用户信息失败...");
			e.printStackTrace();
		}finally{
			dbTool.closeConn(conn);
		}
		return userPhones;
	}

	@Override
	public List<UserPhone> getUserPhonesLikeName(String name) {
		Connection conn = dbTool.getConnection();
		List<UserPhone> userPhones = null;
		try {
			conn.setAutoCommit(false);
			userPhones = userPhoneDao.getUserPhonesLikeName(name, conn);
			for(UserPhone up : userPhones){
				up.setDept(deptDao.getDeptById(up.getDept().getId(), conn));
			}
			conn.commit();
			conn.setAutoCommit(true);
		} catch (Exception e) {
			try {
				conn.rollback();
				conn.setAutoCommit(true);
			} catch (SQLException e1) {
				System.out.println("回滚失败...");
				e1.printStackTrace();
			}
			System.out.println("查询相关名称的用户信息失败...");
			e.printStackTrace();
		}finally{
			dbTool.closeConn(conn);
		}
		return userPhones;
	}

	@Override
	public List<UserPhone> getUserPhonesByIds(int[] ids) {
		Connection conn = dbTool.getConnection();
		List<UserPhone> userPhones = new ArrayList<UserPhone>();
		try {
			conn.setAutoCommit(false);
			for(int id : ids){
				userPhones.add(userPhoneDao.getUserPhonesById(id, conn));
			}
			for(UserPhone up : userPhones){
				up.setDept(deptDao.getDeptById(up.getDept().getId(), conn));
			}
			conn.commit();
			conn.setAutoCommit(true);
		} catch (Exception e) {
			try {
				conn.rollback();
				conn.setAutoCommit(true);
			} catch (SQLException e1) {
				System.out.println("回滚失败...");
				e1.printStackTrace();
			}
			System.out.println("批量获取用户信息失败...");
			e.printStackTrace();
		}finally{
			dbTool.closeConn(conn);
		}
		return userPhones;
	}

	@Override
	public UserPhone getUserPhoneById(int id) {
		Connection conn = dbTool.getConnection();
		UserPhone userPhone = null;
		try {
			conn.setAutoCommit(false);
			userPhone = userPhoneDao.getUserPhonesById(id, conn);
			if(userPhone != null){
				userPhone.setDept(deptDao.getDeptById(userPhone.getDept().getId(), conn));
			}
			conn.commit();
			conn.setAutoCommit(true);
		} catch (Exception e) {
			try {
				conn.rollback();
				conn.setAutoCommit(true);
			} catch (SQLException e1) {
				System.out.println("回滚失败...");
				e1.printStackTrace();
			}
			System.out.println("查询某个用户信息失败...");
			e.printStackTrace();
		}finally{
			dbTool.closeConn(conn);
		}
		return userPhone;
	}

	@Override
	public boolean updateUserPhone(UserPhone userPhone) {
		Connection conn = dbTool.getConnection();
		boolean flag = false;
		try {
			conn.setAutoCommit(false);
			userPhoneDao.updateUserPhone(userPhone, conn);
			conn.commit();
			conn.setAutoCommit(true);
			flag = true;
		} catch (Exception e) {
			try {
				conn.rollback();
				conn.setAutoCommit(true);
			} catch (SQLException e1) {
				System.out.println("回滚失败...");
				e1.printStackTrace();
			}
			System.out.println("更新用户信息失败...");
			e.printStackTrace();
		}finally{
			dbTool.closeConn(conn);
		}
		return flag;
	}

	@Override
	public boolean addUserPhone(UserPhone userPhone) {
		Connection conn = dbTool.getConnection();
		boolean flag = false;
		try {
			conn.setAutoCommit(false);
			userPhoneDao.addUserPhone(userPhone, conn);
			conn.commit();
			conn.setAutoCommit(true);
			flag = true;
		} catch (Exception e) {
			try {
				conn.rollback();
				conn.setAutoCommit(true);
			} catch (SQLException e1) {
				System.out.println("回滚失败...");
				e1.printStackTrace();
			}
			System.out.println("添加用户信息失败...");
			e.printStackTrace();
		}finally{
			dbTool.closeConn(conn);
		}
		return flag;
	}

	@Override
	public UserPhone getUserPhonesByNo(String no) {
		Connection conn = dbTool.getConnection();
		UserPhone userPhone = null;
		try {
			conn.setAutoCommit(false);
			userPhone = userPhoneDao.getUserPhonesByNo(no, conn);
			conn.commit();
			conn.setAutoCommit(true);
		} catch (Exception e) {
			try {
				conn.rollback();
				conn.setAutoCommit(true);
			} catch (SQLException e1) {
				System.out.println("回滚失败...");
				e1.printStackTrace();
			}
			System.out.println("通过号码查用户信息失败...");
			e.printStackTrace();
		}finally{
			dbTool.closeConn(conn);
		}
		return userPhone;
	}

	@Override
	public List<UserPhone> getUserPhonesByDN(int dept_id, String u_name) {
		Connection conn = dbTool.getConnection();
		List<UserPhone> userPhones = new ArrayList<UserPhone>();
		try {
			conn.setAutoCommit(false);
			userPhones = userPhoneDao.getUserPhonesByDN(dept_id, u_name, conn);
			for(UserPhone up : userPhones){
				up.setDept(deptDao.getDeptById(up.getDept().getId(), conn));
			}
			conn.commit();
			conn.setAutoCommit(true);
		} catch (Exception e) {
			try {
				conn.rollback();
				conn.setAutoCommit(true);
			} catch (SQLException e1) {
				System.out.println("回滚失败...");
				e1.printStackTrace();
			}
			System.out.println("通过姓名部门查用户信息失败...");
			e.printStackTrace();
		}finally{
			dbTool.closeConn(conn);
		}
		return userPhones;
	}

	@Override
	public boolean delUserPhone(int u_id) {
		Connection conn = dbTool.getConnection();
		boolean flag = false;
		try {
			conn.setAutoCommit(false);
			userPhoneDao.delUserPhone(u_id, conn);
			conn.commit();
			conn.setAutoCommit(true);
			flag = true;
		} catch (Exception e) {
			try {
				conn.rollback();
				conn.setAutoCommit(true);
			} catch (SQLException e1) {
				System.out.println("回滚失败...");
				e1.printStackTrace();
			}
			System.out.println("删除用户信息失败...");
			e.printStackTrace();
		}finally{
			dbTool.closeConn(conn);
		}
		return flag;
	}

	@Override
	public List<UserPhone> getUserPhonesByDNCS(int dept_id, String u_name, String u_company, String u_phoneno) {
		Connection conn = dbTool.getConnection();
		List<UserPhone> userPhones = new ArrayList<UserPhone>();
		try {
			conn.setAutoCommit(false);
			userPhones = userPhoneDao.getUserPhonesByDNCS(dept_id, u_name, u_company, u_phoneno, conn);
			for(UserPhone up : userPhones){
				up.setDept(deptDao.getDeptById(up.getDept().getId(), conn));
			}
			conn.commit();
			conn.setAutoCommit(true);
		} catch (Exception e) {
			try {
				conn.rollback();
				conn.setAutoCommit(true);
			} catch (SQLException e1) {
				System.out.println("回滚失败...");
				e1.printStackTrace();
			}
			System.out.println("通过公司部门姓名手机号查询用户信息失败...");
			e.printStackTrace();
		}finally{
			dbTool.closeConn(conn);
		}
		return userPhones;
	}

	@Override
	public List<String> getCompanyNames() {
		Connection conn = dbTool.getConnection();
		List<String> compNames = null;
		try {
			conn.setAutoCommit(false);
			compNames = userPhoneDao.getCompanyNames(conn);
			conn.commit();
			conn.setAutoCommit(true);
		} catch (Exception e) {
			try {
				conn.rollback();
				conn.setAutoCommit(true);
			} catch (SQLException e1) {
				System.out.println("回滚失败...");
				e1.printStackTrace();
			}
			System.out.println("获取所有公司名称失败...");
			e.printStackTrace();
		}finally{
			dbTool.closeConn(conn);
		}
		return compNames;
	}
	
}
